﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using GMapNET;

namespace UGSV_GUI
{
    public class Server
    {
        public static Thread serverThread;

        //global settings
        private bool serveRequests = true;

        //global vars
        private HttpListener listenerHttp = null;
        private State currentState;

        public Server(State ssdas)
        {
            currentState = ssdas;
        }

        public void startServer()
        {
            //spawn server thread, then we are done
            serverThread = new Thread(new ThreadStart(internalServer));
            serverThread.Start();
        }

        public void stopServer()
        {
            serverThread.Abort();
            serveRequests = false;
            //serverThread = null;
        }

        public void internalServer()
        {

            //only supported on xp sp2 and above
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("WebServer is not compatible with this operating system");
                while (true) ; //hang
            }

            //now to load settings and start service
            if (true)
            {
                try
                {
                    Console.WriteLine("Running Server...");
                    Console.WriteLine("Press ctrl c to stop.");

                    //new httplistener
                    listenerHttp = new HttpListener();

                    //listen on http port 441
                    listenerHttp.Prefixes.Add("http://+:441/listener/");

                    listenerHttp.Start();

                    Console.WriteLine("Server Started and listening");

                    while (serveRequests)
                    {
                        HttpListenerContext context = listenerHttp.GetContext();
                        HttpListenerRequest Request = context.Request;
                        HttpListenerResponse Response = context.Response;

                        byte[] bOutput = System.Text.Encoding.UTF8.GetBytes(currentState.getOutput());

                        Response.ContentType = "text/html";
                        Response.ContentLength64 = bOutput.Length;

                        Stream OutputStream = Response.OutputStream;
                        OutputStream.Write(bOutput, 0, bOutput.Length);
                        OutputStream.Close();

                        //Response.Close();
                        //exit condition
                        if (!serveRequests)
                            return;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception trying to start tcplistener or server thread :" + e);
                }
            }
            else
            {
                Console.WriteLine("Failed to load TrackerServer.config, Press Ctrl c to exit");
                while (true) ; //hang
            }

        }

    }
}
